<script lang="ts">
import BookShelf from './book_shelf.vue'
export default {
    components:{
        shelf:BookShelf
    },
    data() {
        return {
            book_dict:new Map(),
            keyword: ''
        }
    },
    methods: {
        search_books() {
            if (this.keyword.length == 0) {
                alert('客官，你还没有填关键词呢！')
                return
            }
            const api = 'http://localhost:9999/search?keyword='
            fetch(api + this.keyword)
                .then(res => res.json())
                .then(data => {
                    console.log('Get search result:')
                    console.dir(data)
                    this.book_dict.clear()
                    data.forEach(element => {
                        this.book_dict.set(element.id,element)
                    });
                })
        }
    },
    emits: [],
    mounted() {
        this.keyword = 'ni'
        // this.search_books()
    }
}
</script>

<template>
    <div class="top-panel">
        <div class="search-panel">
            <input v-model="keyword" type="text" placeholder="客官想找点啥？" />
            <a class="btn" @click="search_books">搜索</a>
        </div>
        <div class="result-panel">
            <shelf :collection="book_dict"></shelf>
        </div>
    </div>
</template>

<style scoped lang="scss">
.top-panel {
    display: grid;
    grid-template-rows: auto 1fr;
    width: 100%;
    height: 100%;
    justify-content: center;
    align-items: center;
}
.search-panel {
    height: 100px;
    display: grid;
    margin-top: 50px;
    grid-template-rows: 1fr 1fr;
    justify-content: center;
}

.search-panel .btn {
    border: 1px solid darkcyan;
    border-radius: 40px;
    font-size: 1.2rem;
    height: 30px;
    width: 100px;
    text-align: center;
    line-height: 30px;
    font-weight: 600;
    justify-self: center;
    &:hover {
        background-color: darkcyan;
        box-shadow: 0 0 2px 4px darkcyan;
        color: #333;
    }
}

.book-shelf {
    display: flex;
    flex-wrap: wrap;
}
.book-card {
    flex-basis: 300px;
    flex-shrink: 0;
    flex-grow: 0;
    width: 300px;
    height: 300px;
    display: grid;
    grid-template-columns: 200px 1fr;
    margin: 10px;
    position: relative;
    text-shadow: 1px 1px 4px cyan;

    img {
        display: block;
        width: 200px;
        height: 300px;
        background-color: cadetblue;
    }
}
.book-title {
    position: absolute;
    left: 0px;
    bottom: 0px;
    width: 200px;
    text-align: center;
}
.book-panel {
    display: grid;
    grid-template-rows: 1fr, 1fr;
}
.book-chapter {
    display: grid;
    grid-template-rows: 1fr, 1fr;
    span {
        display: block;
        width: 100%;
        text-align: center;
    }
}
.book-chapter-label {
    font-size: 1.4rem;
    font-weight: 600;
}
.book-chapter-count {
    font-size: 1.5rem;
    font-weight: 900;
}
.book-operation {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: center;
}
a.btn {
    padding: 5px 20px;
    // font-size: 1.2rem;
    border: 1px solid darkcyan;
    border-radius: 5px;
    margin-top: 10px;
    font-weight: 600;
    text-shadow: none;
    &:hover {
        background-color: darkcyan;
        color: #333;
    }
}
.btn.danger {
    border-color: deeppink;
    &:hover {
        background-color: deeppink;
    }
}
.progress-label {
    width: 100%;
    text-align: center;
    display: block;
    font-size: 1.2rem;
    font-weight: 600;
    margin: 10px 0 2px;
}
.book-progress {
    width: 100%;
    margin-left: 5px;
    height: 20px;
    position: relative;
    border-radius: 20px;
    border: 1px solid darkcyan;
    overflow: hidden;
    .progress-bar {
        display: block;
        height: 100%;
        width: 20%;
        background-color: darkcyan;
    }
    .progress-value {
        display: block;
        position: absolute;
        text-align: center;
        left: 0;
        top: 0;
        width: 100%;
        font-weight: 600;
    }
}
</style>
